<!DOCTYPE HTML>
<html>
<head>
<!--
test/aperiodic/deflate.html

this file draws tilings by kites and darts using deflation.

-->
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="width=320;height=356;user-scalable=no;initial-scale=1.0;" />
<!-- <meta name="apple-mobile-web-app-capable" content="yes" /> -->
<!-- <meta name="apple-mobile-web-app-status-bar-style" content="black" /> -->
<style>
	body { padding:0px; margin:0px; color: white; background-color: gray; }
	input { 
		font-size:16px;
		padding: 0px;
	}
	* {font-family: sans-serif}
	form { padding:0px; margin:0px; }
</style>
<script src="../../lib/canvas.js" /></script>
<script>
	var phi;
	var u;
	var opt = new Array();

	function draw_something() {
		draw_deuce(0,0,0,4)
	}

	function draw_something_else() {
		opt = new Array(); 
		opt.border = true;
		ctx.lineWidth = 3;
		ctx.strokeStyle = 'white';
		draw_deuce(0,0,0,0)
	}

	function init() {
		canvas_init();
		phi = Math.sqrt(5) + 1;
		phi /= 2;
		u = 80;
		ctx.strokeStyle = 'white';
		ctx.lineWidth = phi;
		ctx.save();
		ctx.translate(canvas.width / 2, canvas.height / 2);
		opt = new Array(); opt.half = true;
		ctx.strokeStyle = 'rgb(31, 0, 63)';
		draw_something();
		opt = new Array(); opt.border = true;
		ctx.strokeStyle = 'rgb(0, 0, 255)';
		draw_something();
		opt = new Array(); opt.arc_a = true;
		ctx.strokeStyle = 'rgb(255, 0, 0)';
		//draw_something();
		opt = new Array(); opt.arc_b = true;
		ctx.strokeStyle = 'rgb(0, 255, 0)';
		//draw_something();

		draw_something_else();

		ctx.restore();
	}

	function draw_cartwheel_1 (x, y, theta) {
		ctx.save();
		ctx.translate(x, y);
		ctx.rotate(theta);
		var t = 0;
		var r = u * phi;

		var n = draw_dart(0, r, t);
		var o = n.kites.tl;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.kites.bl;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.kites.tl;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.darts.tl;
		n = draw_dart(o.x, o.y, o.theta);
		o = n.kites.br;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.kites.tr;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.darts.bl;
		n = draw_dart(o.x, o.y, o.theta);
		o = n.kites.tr;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.kites.bl;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.kites.tr;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.darts.bl;
		n = draw_dart(o.x, o.y, o.theta);
		o = n.kites.tr;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.darts.tl;
		n = draw_dart(o.x, o.y, o.theta);
		o = n.kites.br;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.kites.tr;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.kites.br;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.kites.br;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.kites.br;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.darts.tl;
		n = draw_dart(o.x, o.y, o.theta);
		o = n.kites.bl;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.darts.tr;
		n = draw_dart(o.x, o.y, o.theta);
		o = n.darts.bl;
		n = draw_dart(o.x, o.y, o.theta);
		o = n.kites.tl;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.darts.br;
		n = draw_dart(o.x, o.y, o.theta);
		o = n.kites.tr;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.kites.br;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.kites.br;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.darts.tr;
		n = draw_dart(o.x, o.y, o.theta);
		o = n.darts.bl;
		n = draw_dart(o.x, o.y, o.theta);
		o = n.kites.tl;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.darts.br;
		n = draw_dart(o.x, o.y, o.theta);
		o = n.kites.tr;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.kites.br;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.kites.br;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.kites.br;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.darts.tl;
		n = draw_dart(o.x, o.y, o.theta);
		o = n.kites.bl;
		n = draw_kite(o.x, o.y, o.theta);
		o = n.darts.tr;
		n = draw_dart(o.x, o.y, o.theta);
		o = n.darts.bl;
		n = draw_dart(o.x, o.y, o.theta);

		ctx.restore();
	}

	function inflate_kite(x, y, theta, times) {
		if (times <= 0) {
			return draw_kite(x, y, theta);
		}
		var t = draw_kite(x, y, theta, true);
		var i = times - 1;

		ctx.save();
		ctx.translate(x, y);
		ctx.rotate(theta);
		ctx.scale(1/phi, 1/phi);

		var n = inflate_dart(0, 0, Math.PI / 5, i);
		o = n.darts.bl;
		n = inflate_dart(o.x, o.y, o.theta, i);
		o = n.kites.tl;
		n = inflate_kite(o.x, o.y, o.theta, i);
		o = n.kites.bl;
		n = inflate_kite(o.x, o.y, o.theta, i);
		o = n.kites.tl;
		n = inflate_kite(o.x, o.y, o.theta, i);
		o = n.kites.bl;
		n = inflate_kite(o.x, o.y, o.theta, i);

		ctx.restore();

		return t;
	}

	function inflate_dart(x, y, theta, times) {
		if (times <= 0) {
			return draw_dart(x, y, theta);
		}
		var t = draw_dart(x, y, theta, true);
		var i = times - 1;

		ctx.save();
		ctx.translate(x, y);
		ctx.rotate(theta);
		ctx.scale(1/phi, 1/phi);


		var n = inflate_kite(0, 0, 2 * Math.PI / 5, i);
		o = n.darts.tl;
		n = inflate_dart(o.x, o.y, o.theta, i);
		o = n.kites.tr;
		n = inflate_kite(o.x, o.y, o.theta, i);
		o = n.darts.tl;
		n = inflate_dart(o.x, o.y, o.theta, i);
		o = n.kites.tr;
		n = inflate_kite(o.x, o.y, o.theta, i);

		ctx.restore();

		return t;
	}

	function draw_kite(x, y, theta, hide) {

		// return a list of possible neighboring tiles
		var o = new Array();
		o.kites = new Array();
		o.darts = new Array();
		o.kites.bl = {x: x, y:y, theta: theta - 2 * Math.PI/5};
		o.kites.br = {x: x, y:y, theta: theta + 2 * Math.PI/5};
		o.kites.tl = {x: x, y:y, theta: theta + 4 * Math.PI/5};
		o.kites.tr = {x: x, y:y, theta: theta - 4 * Math.PI/5};
		var hyp = 2 * u * (1 + phi) * Math.cos(Math.PI/10);
		var t;
		t = - theta - 9 * Math.PI/10;
		o.kites.tl.x = x + hyp * Math.sin(t);
		o.kites.tl.y = y + hyp * Math.cos(t);
		t = - theta + 9 * Math.PI/10;
		o.kites.tr.x = x + hyp * Math.sin(t);
		o.kites.tr.y = y + hyp * Math.cos(t);

		hyp = u * (1 + phi);

		o.darts.bl = {x: x, y:y, theta: theta + Math.PI};
		o.darts.br = {x: x, y:y, theta: theta + Math.PI};

		t = - theta - 4 * Math.PI/5;
		o.darts.bl.x = x + hyp * Math.sin(t);
		o.darts.bl.y = y + hyp * Math.cos(t);

		t = - theta + 4 * Math.PI/5;
		o.darts.br.x = x + hyp * Math.sin(t);
		o.darts.br.y = y + hyp * Math.cos(t);

		o.darts.tl = {x: x, y:y, theta: theta + 4 * Math.PI / 5};
		o.darts.tr = {x: x, y:y, theta: theta - 4 * Math.PI / 5};

		hyp = u * (1 + 2 * phi);

		t = - theta - 4 * Math.PI/5;
		o.darts.tl.x = x + hyp * Math.sin(t);
		o.darts.tl.y = y + hyp * Math.cos(t);

		t = - theta + 4 * Math.PI/5;
		o.darts.tr.x = x + hyp * Math.sin(t);
		o.darts.tr.y = y + hyp * Math.cos(t);

		if (null != hide) { return o; }

		if (null == opt || opt.half) {
			ctx.save();
			ctx.translate(x, y);
			ctx.rotate(theta + Math.PI);
			ctx.beginPath();
			ctx.moveTo(0, 0);
			ctx.lineTo(0, u * (1 + phi));
			ctx.stroke();
			ctx.restore();
		}

		if (null == opt || opt.border) {
		ctx.save();
		ctx.translate(x, y);
		ctx.rotate(theta + Math.PI);
		ctx.beginPath();
		ctx.moveTo(0, 0);
		ctx.rotate(Math.PI/5);
		ctx.translate(0, u * (phi + 1));
		ctx.lineTo(0, 0);
		ctx.rotate(-3 * Math.PI/5);
		ctx.translate(0, u * phi);
		ctx.lineTo(0, 0);
		ctx.rotate(- Math.PI/5);
		ctx.translate(0, u * phi);
		ctx.lineTo(0, 0);
		ctx.rotate(-3 * Math.PI/5);
		ctx.translate(0, u * (1 + phi));
		ctx.lineTo(0, 0);
		ctx.stroke();
		ctx.restore();
		}

		if (null == opt || opt.arc_a) {
		ctx.save();
		ctx.translate(x, y);
		ctx.moveTo(0, 0);
		ctx.rotate(theta + Math.PI);
		ctx.rotate(3 * Math.PI/10);
		ctx.beginPath();
		ctx.arc(0, 0, u * phi, 0, 2 * Math.PI/5, false);
		ctx.stroke();
		ctx.restore();
		}

		if (null == opt || opt.arc_b) {
		ctx.save();
		ctx.translate(x, y);
		ctx.moveTo(0, 0);
		ctx.rotate(theta + Math.PI);
		ctx.translate(0, u * (1 + phi));
		ctx.rotate(11 * Math.PI/10);
		ctx.beginPath();
		ctx.arc(0, 0, u , 0, 4 * Math.PI/5, false);
		ctx.stroke();
		ctx.restore();
		}

		return o;
	}

	function draw_dart(x, y, theta, hide) {
		// return a list of possible neighboring tiles
		var o = new Array();

		o.kites = new Array();
		o.darts = new Array();
		o.darts.bl = {x: x, y:y, theta: theta - 2 * Math.PI / 5};
		o.darts.br = {x: x, y:y, theta: theta + 2 * Math.PI / 5};
		o.darts.tl = o.darts.bl;
		o.darts.tr = o.darts.br;

		o.kites.bl = {x: x, y:y, theta: theta + Math.PI};
		o.kites.br = {x: x, y:y, theta: theta + Math.PI};

		var hyp;
		var t;

		hyp = u * (1 + phi);

		t = - theta - 4 * Math.PI / 5;
		o.kites.bl.x = x + hyp * Math.sin(t);
		o.kites.bl.y = y + hyp * Math.cos(t);

		t = - theta + 4 * Math.PI / 5;
		o.kites.br.x = x + hyp * Math.sin(t);
		o.kites.br.y = y + hyp * Math.cos(t);

		o.kites.tl = {x: x, y:y, theta: theta - 4 * Math.PI / 5};
		o.kites.tr = {x: x, y:y, theta: theta + 4 * Math.PI / 5};

		hyp = u * (1 + 2 * phi);
		t = - theta + Math.PI;
		o.kites.tl.x = x + hyp * Math.sin(t);
		o.kites.tl.y = y + hyp * Math.cos(t);
		o.kites.tr.x = x + hyp * Math.sin(t);
		o.kites.tr.y = y + hyp * Math.cos(t);

		if (null != hide) { return o; }

		if (null == opt || opt.half) {
			ctx.save();
			ctx.translate(x, y);
			ctx.rotate(theta + Math.PI);
			ctx.beginPath();
			ctx.moveTo(0, 0);
			ctx.lineTo(0, u * phi);
			ctx.stroke();
			ctx.restore();
		}

		if (null == opt || opt.border) {
		ctx.save();
		ctx.translate(x, y);
		ctx.rotate(theta + Math.PI);

		ctx.beginPath();
		ctx.moveTo(0, 0);
		ctx.rotate(Math.PI/5);
		ctx.translate(0, u * (phi + 1));
		ctx.lineTo(0, 0);
		ctx.rotate(-4 * Math.PI/5);
		ctx.translate(0, u * phi);
		ctx.lineTo(0, 0);
		ctx.rotate( Math.PI/5);
		ctx.translate(0, u * phi);
		ctx.lineTo(0, 0);
		ctx.rotate(-4 * Math.PI/5);
		ctx.translate(0, u * (1 + phi));
		ctx.lineTo(0, 0);
		ctx.stroke();
		ctx.restore();
		}

		if (null == opt || opt.arc_a) {
		ctx.save();
		ctx.translate(x, y);
		ctx.moveTo(0, 0);
		ctx.rotate(theta + Math.PI);
		ctx.rotate(3 * Math.PI/10);
		ctx.beginPath();
		ctx.arc(0, 0, u, 0, 2 * Math.PI/5, false);
		ctx.stroke();
		ctx.restore();
		}

		if (null == opt || opt.arc_b) {
		ctx.save();
		ctx.translate(x, y);
		ctx.moveTo(0, 0);
		ctx.rotate(theta + Math.PI);
		ctx.translate(0, u * (1 + 1/ phi));
		ctx.rotate(9 * Math.PI/10);
		ctx.beginPath();
		ctx.arc(0, 0, u / phi, 0, 6 * Math.PI/5, false);
		ctx.stroke();
		ctx.restore();
		}

		return o;
	}

	function new_tiles(x, y, theta) {
		var t = new Array();
		t.x = x;
		t.y = y;
		t.theta = theta;
		t.kites = new Array();
		t.darts = new Array();

		return t;
	}

	function draw_ace(x, y, theta, inflate) {
		var iter;
		if (null == inflate) {
			iter = 0;
		} else {
			iter = inflate;
		}

		ctx.save();
		ctx.translate(x, y);
		ctx.rotate(theta);
		var t = 0;
		var r = u * phi;


		var n = inflate_dart(0, r, t, iter);
		inflate_kite(n.kites.tl.x, n.kites.tl.y, n.kites.tl.theta, iter);
		inflate_kite(n.kites.tr.x, n.kites.tr.y, n.kites.tr.theta, iter);
		ctx.restore();
	}

	function draw_deuce(x, y, theta, inflate) {
		var iter;
		if (null == inflate) {
			iter = 0;
		} else {
			iter = inflate;
		}

		ctx.save();
		ctx.translate(x, y);
		ctx.rotate(theta);

		var t = 4 * Math.PI / 5;
		var r = - u * (1 + phi);

		var n = inflate_dart(0, r, t, iter);
		inflate_kite(n.kites.tr.x, n.kites.tr.y, n.kites.tr.theta, iter);
		n = inflate_dart(n.darts.br.x, n.darts.br.y, n.darts.br.theta, iter);
		inflate_kite(n.kites.tl.x, n.kites.tl.y, n.kites.tl.theta, iter);

		ctx.restore();
	}

	function draw_sun(x, y, theta, inflate) {
		var iter;
		if (null == inflate) {
			iter = 0;
		} else {
			iter = inflate;
		}

		ctx.save();
		ctx.translate(x, y);
		ctx.rotate(theta);
		var t = Math.PI;
		var nt = 2 * Math.PI / 5;
		for (var i = 0; i < 5; ++i) {
			inflate_kite(0, 0, t, iter);
			t += nt;
		}
		ctx.restore();
	}

	function draw_star(x, y, theta, inflate) {
		var iter;
		if (null == inflate) {
			iter = 0;
		} else {
			iter = inflate;
		}

		ctx.save();
		ctx.translate(x, y);
		ctx.rotate(theta);
		var t = 0;
		var nt = 2 * Math.PI / 5;
		for (var i = 0; i < 5; ++i) {
			inflate_dart(0, 0, t, iter);
			t += nt;
		}
		ctx.restore();
	}

	function draw_jack(x, y, theta, inflate) {
		var iter;
		if (null == inflate) {
			iter = 0;
		} else {
			iter = inflate;
		}

		ctx.save();
		ctx.translate(x, y);
		ctx.rotate(theta);

		var t = Math.PI / 5;
		var r = 0;

		var n = inflate_kite(0, r, t, iter);
		var o = n.darts.br;
		n = inflate_dart(o.x, o.y, o.theta, iter);
		o = n.kites.tr;
		n = inflate_kite(o.x, o.y, o.theta, iter);
		o = n.darts.tl;
		n = inflate_dart(o.x, o.y, o.theta, iter);
		o = n.kites.bl;
		n = inflate_kite(o.x, o.y, o.theta, iter);
		ctx.restore();
	}

	function draw_queen(x, y, theta, inflate) {
		var iter;
		if (null == inflate) {
			iter = 0;
		} else {
			iter = inflate;
		}

		ctx.save();
		ctx.translate(x, y);
		ctx.rotate(theta);

		var n = inflate_dart(0, 0, 0, iter);
		var o = n.kites.br;
		n = inflate_kite(o.x, o.y, o.theta, iter);
		o = n.kites.tr;
		n = inflate_kite(o.x, o.y, o.theta, iter);
		o = n.kites.bl;
		n = inflate_kite(o.x, o.y, o.theta, iter);
		o = n.kites.tr;
		n = inflate_kite(o.x, o.y, o.theta, iter);
		ctx.restore();
	}

	function draw_king(x, y, theta, inflate) {
		var iter;
		if (null == inflate) {
			iter = 0;
		} else {
			iter = inflate;
		}

		ctx.save();
		ctx.translate(x, y);
		ctx.rotate(theta);

		var n = inflate_dart(0, 0, 0, iter);
		var o = n.darts.br;
		n = inflate_dart(o.x, o.y, o.theta, iter);
		o = n.kites.br;
		n = inflate_kite(o.x, o.y, o.theta, iter);
		o = n.kites.tr;
		n = inflate_kite(o.x, o.y, o.theta, iter);
		o = n.darts.bl;
		n = inflate_dart(o.x, o.y, o.theta, iter);
		ctx.restore();
	}

</script>
</head>
<body onload="init()">
	<canvas id="canvas" class="canvas"  width="640" height="640"></canvas>
</body>
</html>
